<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;

use App\Models\CourseSectionResources;
use App\Services\CourseSectionResourcesService;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class CourseSectionResourcesController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'resource_file' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        CourseSectionResources::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'resource_file' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);
        $courseSectionResources = new CourseSectionResources();
        $courseSectionResources->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [

            'sort' => 'string',
            'order' => 'string',
            'course_id' => 'integer',
            'course_section_id' => 'integer',
            'name' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $name = $request->input('name');
        $course_id = $request->input('course_id');
        $course_section_id = $request->input('course_section_id');


        $list = CourseSectionResourcesService::getInstance()->list($sort,$order,$course_id, $course_section_id, $name);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $courseSectionResources = CourseSectionResources::find($request->id);
        $courseSectionResources->delete();
        return $this->success();
    }

    // 上传
    public function upfile(Request $request) {
        $url = config('filesystems.disks.course.url');
        $default = config('filesystems.disks.course.default');
        $pic =  CourseSectionResourcesService::getInstance()->upfile($request,$url,$default,'course');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }
}
